<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;//导入模型类
use think\Session;

class Pub extends Controller
{
    public function login()
    {
        $this->assign('title', '登录');
        return $this->fetch('login/index');
    }
    public function dologin()
    {
        //接收用户名和密码
        $username = input('username');
        $password = input('password');
        //验证,数据库查询,匹配输入信息
        $data = db('admin')->where('a_username', $username)->find();
        // var_dump($data);
        // die();
        if (!$data) {
            $this->error('用户名不存在！');
            exit;
        }
        //验证密码
        if ($data['a_password'] != md5($password)) {
            $this->error('密码不正确');
            exit;
        }
        /*//把用户信息添加到session
        $mor = session('admin_user', $data);*/


        //根据用户id获取对应的节点信息
         $sql = "select au_controller,au_fun from admin u join admin_role ur on u.id = ur.admin_id join role_auth rn on ur.f_role_id = rn.s_role_id join auth n on rn.auth_id = n.id where u.id = ".$data['id'];
         $list = Db::query($sql);
         /*var_dump($list);
         die();*/

         //控制器名转换为大写
        foreach ($list as $key => $val) {
            $list[$key]['au_controller'] = ucfirst($val['au_controller']);
        }

        $nodelist = array();
        $nodelist['Index'] = array('index');
        //遍历重新拼装
        foreach($list as $v){
            $nodelist[$v['au_controller']][] = $v['au_fun'];
            //把修改和执行修改 添加和执行添加 拼装到一起
            if($v['au_fun']=="edit"){
                $nodelist[$v['au_controller']][]="edited";
            }
            if($v['au_fun']=="add"){
                $nodelist[$v['au_controller']][]="insert";
            }
        }
        // var_dump($nodelist);die();
        $data['node'] = $nodelist;
        //将权限信息与用户信息放置到session中
        session("admin_user", $data);
        // $_SESSION['admin_user']['nodelist'] = $nodelist;
        /*echo '<pre>';
        print_r($_SESSION);
        die();*/
        //跳转到首页
        $this->redirect('Index/index');
        // $this->success('登录成功', 'Index/index');
        
    }
    //退出登陆
    public function logout()
    {
        //清空session
        session('admin_user', null);
        //跳转
        $this->redirect('Index/index');
    }
}